/**
 *	@file	safaril_sub.c
 *	@brief	サファリレイアウトツール　サブ
 *	@author	Miyuki Iwasawa
 *	@date	08.03.13
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/window.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/wordset.h"
#include "system/buflen.h"
#include "gflib/msg_print.h"
#include "gflib/strbuf_family.h"
#include "application/app_tool.h"
#include "savedata/config.h"

#include "safari/safari_sys.h"
#include "application/safari_l.h"

#include "safaril_sys.h"
#include "safaril_sub.h"
#include "safaril_act.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_safari_l.h"
#include "safaril_gra.naix"
#include "safaril_snd_def.h"

//================================================================
//----------------------------------------------------------------
///定義
//----------------------------------------------------------------
//================================================================

///セレクトボックス　ヘッダデータ構造体
static const SELBOX_HEAD_PRM data_sbox_sel = {
	TRUE,SBOX_OFSTYPE_CENTER,	///<ループフラグ、左寄せorセンタリング

	0,	///<項目表示オフセットX座標(dot)

	MFRM_MSG,				///<フレームNo
	SBOX_PAL,				///<フレームパレットID
	0,0,					///oam BG Pri,Software Pri
	SBOX_FCGX,SBOX_WCGX,	///<ウィンドウcgx,フレームcgx
	SBOX_FCGX_SIZ,		///<BMPウィンドウ占有キャラクタ領域サイズ(char)
};

///セレクトボックス表示位置データ
static const SAFARIL_SBOX_POS data_sbox_pos[] = {
 {12,2},{21,2},{10,2},
 {12,9},{21,9},{10,9},
};

#define WORDSET_BUFLEN	(8*2+EOM_SIZE)
#define SAFARIL_BUFLEN	(20*2*2+EOM_SIZE)

static const RECT_HIT_TBL Panel1_TpRect[] = {
//		{0,191,0,255}, ty,by,lx,rx
 {TP_PANEL_PY01,TP_PANEL_PY01+TP_PANEL_SY,TP_PANEL1_PX01,TP_PANEL1_PX01+TP_PANEL_SX},
 {TP_PANEL_PY01,TP_PANEL_PY01+TP_PANEL_SY,TP_PANEL1_PX02,TP_PANEL1_PX02+TP_PANEL_SX},
 {TP_PANEL_PY01,TP_PANEL_PY01+TP_PANEL_SY,TP_PANEL1_PX03,TP_PANEL1_PX03+TP_PANEL_SX},
 {TP_PANEL_PY02,TP_PANEL_PY02+TP_PANEL_SY,TP_PANEL1_PX01,TP_PANEL1_PX01+TP_PANEL_SX},
 {TP_PANEL_PY02,TP_PANEL_PY02+TP_PANEL_SY,TP_PANEL1_PX02,TP_PANEL1_PX02+TP_PANEL_SX},
 {TP_PANEL_PY02,TP_PANEL_PY02+TP_PANEL_SY,TP_PANEL1_PX03,TP_PANEL1_PX03+TP_PANEL_SX},
 {TP_BACK_PY,TP_BACK_PY+TP_BACK_SY,TP_BACK_PX,TP_BACK_PX+TP_BACK_SX},
 {RECT_HIT_END,0,0,0}
};

static const RECT_HIT_TBL Panel2_TpRect[] = {
//		{0,191,0,255}, ty,by,lx,rx
 {TP_PANEL_PY01,TP_PANEL_PY01+TP_PANEL_SY,TP_PANEL2_PX01,TP_PANEL2_PX01+TP_PANEL_SX},
 {TP_PANEL_PY01,TP_PANEL_PY01+TP_PANEL_SY,TP_PANEL2_PX02,TP_PANEL2_PX02+TP_PANEL_SX},
 {TP_PANEL_PY01,TP_PANEL_PY01+TP_PANEL_SY,TP_PANEL2_PX03,TP_PANEL2_PX03+TP_PANEL_SX},
 {TP_PANEL_PY02,TP_PANEL_PY02+TP_PANEL_SY,TP_PANEL2_PX01,TP_PANEL2_PX01+TP_PANEL_SX},
 {TP_PANEL_PY02,TP_PANEL_PY02+TP_PANEL_SY,TP_PANEL2_PX02,TP_PANEL2_PX02+TP_PANEL_SX},
 {TP_PANEL_PY02,TP_PANEL_PY02+TP_PANEL_SY,TP_PANEL2_PX03,TP_PANEL2_PX03+TP_PANEL_SX},
 {TP_BACK_PY,TP_BACK_PY+TP_BACK_SY,TP_BACK_PX,TP_BACK_PX+TP_BACK_SX},
 {TP_ALLOW_PY,TP_ALLOW_PY+TP_ALLOW_SY,TP_ALLOWL_PX,TP_ALLOWL_PX+TP_ALLOW_SX},
 {TP_ALLOW_PY,TP_ALLOW_PY+TP_ALLOW_SY,TP_ALLOWR_PX,TP_ALLOWR_PX+TP_ALLOW_SX},
 {RECT_HIT_END,0,0,0}
};

#define INFO_FCOL0_BASE	(0xC)
#define INFO_FCOL0	((GF_PRINTCOLOR_MAKE(0x8,0xB,INFO_FCOL0_BASE)))
#define INFO_FCOL1	((GF_PRINTCOLOR_MAKE(0xB,0xA,0x8)))
#define INFO_FCOL2	((GF_PRINTCOLOR_MAKE(0xB,0xA,0x9)))
static const GF_PRINTCOLOR InfoWinColor[2] = {
 INFO_FCOL1,INFO_FCOL2,
};
#define GetInfoCol(no)		(InfoWinColor[no])
#define GetInfoBaseCol(no)	(GF_PRINTCOLOR_GET_GROUND(InfoWinColor[no]))

//================================================================
//----------------------------------------------------------------
///プロトタイプ
//----------------------------------------------------------------
//================================================================
static void imodechg_to_key(SAFARIL_WORK* wk);
static void imodechg_to_touch(SAFARIL_WORK* wk);
static BOOL input_change(SAFARIL_WORK* wk);
static int keyin_Block(SAFARIL_WORK* wk);
static int touchin_Block(SAFARIL_WORK* wk);
static int keyin_Change(SAFARIL_WORK* wk);
static int touchin_Change(SAFARIL_WORK* wk);
static int keyin_Sort(SAFARIL_WORK* wk);
static int touchin_Sort(SAFARIL_WORK* wk);
static void sub_SBoxBlockSet(SAFARIL_WORK* wk);
static void sub_SBoxChangeSet(SAFARIL_WORK* wk);

static void sub_VBlank(void* work);
static void sub_BGLInit(SAFARIL_WORK* wk);
static void sub_BGLRelease(SAFARIL_WORK* wk);
static void sub_2DResInit(SAFARIL_WORK* wk);
static void sub_2DResRelease(SAFARIL_WORK* wk);
static void sub_MsgDataInit(SAFARIL_WORK* wk);
static void sub_MsgDataRelease(SAFARIL_WORK* wk);
static void sub_BmpWinAdd(SAFARIL_WORK* wk);
static void sub_BmpWinDelete(SAFARIL_WORK* wk);
static void sub_SelectListInit(SAFARIL_WORK* wk);
static void sub_SelectListRelease(SAFARIL_WORK* wk);
static void sub_ActResInit(SAFARIL_WORK* wk);
static void sub_ActResRelease(SAFARIL_WORK* wk);
static void sub_MakeActor(SAFARIL_WORK* wk);
static void sub_DeleteActor(SAFARIL_WORK* wk);
static void sub_MakeFontOam(SAFARIL_WORK* wk);
static void sub_DelFontOam(SAFARIL_WORK* wk);

static void sub_ExpWinFrameDraw(SAFARIL_WORK* wk,u8 pat);
static void sub_ExpWinDraw(SAFARIL_WORK* wk,u8 widx,u8 midx,u8 draw_f);
static void sub_PageViewSet(SAFARIL_WORK* wk);
static void sub_CursorVisibleSet(SAFARIL_WORK* wk,u8 idx,u8 pat,BOOL draw_f);
static void sub_CursorPosSet(SAFARIL_WORK* wk,u8 idx,u8 pos);
static void sub_ScrnFilterSet(SAFARIL_WORK* wk,u8 frame,u8 pos,BOOL on_f);
static void sub_MakePanelScreen(SAFARIL_WORK* wk);
static void sub_SetPanelChar(SAFARIL_WORK* wk,ARCHANDLE* handel,u8 pos,u8 panel);
static void sub_MakePanelCharBuffer(SAFARIL_WORK* wk,u8 pos,u8 panel);
static void sub_InitialDraw(SAFARIL_WORK* wk);
static void sub_BlockInfoWinBaseDraw(SAFARIL_WORK* wk);
static void sub_BlockInfoWinDraw(SAFARIL_WORK* wk,u8 block_id);
static void sub_BlockInfoWinChangeDraw(SAFARIL_WORK* wk);

static void sub_SetViewToSortFromBlock(SAFARIL_WORK* wk);
static void sub_SetViewToBlockFromSort(SAFARIL_WORK* wk);
static void eff_BCScrollInit(SAFARIL_WORK* wk);
static BOOL eff_BCScroll(SAFARIL_WORK* wk);
static void eff_PageScrollInit(SAFARIL_WORK* wk);
static BOOL eff_PageScroll(SAFARIL_WORK* wk);
static BOOL eff_PanelChange(SAFARIL_WORK* wk,u8 dir);

//================================================================
//----------------------------------------------------------------
///プログラムエリア
//----------------------------------------------------------------
//================================================================

//----------------------------------------------------------------
///グローバルエリア
//----------------------------------------------------------------

/**
 *	@brief	アプリケーション初期化
 *
 *	@retval	TRUE	初期化終了
 *	@retval FALSE	初期化中
 */
BOOL SafariLSeq_AppInit(SAFARIL_WORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		//Blank関数リセット
		sys_VBlankFuncChange(NULL, NULL);
		sys_HBlankIntrStop();	//HBlank割り込み停止

		GF_Disp_GX_VisibleControlInit();
		GF_Disp_GXS_VisibleControlInit();
		GX_SetVisiblePlane(0);
		GXS_SetVisiblePlane(0);
		WIPE_SetBrightness(WIPE_DISP_MAIN,WIPE_FADE_BLACK);
		WIPE_SetBrightness(WIPE_DISP_SUB,WIPE_FADE_BLACK);
		WIPE_ResetWndMask(WIPE_DISP_MAIN);
		WIPE_ResetWndMask(WIPE_DISP_SUB);

		//タッチパネル初期化
		InitTPSystem();
		InitTPNoBuff(4);
		break;
	case 1:
		sub_BGLInit(wk);
		sub_2DResInit(wk);
		sub_MsgDataInit(wk);
		sub_BmpWinAdd(wk);
		sub_SelectListInit(wk);
		sub_ActResInit(wk);
		sub_MakeActor(wk);
		break;
	case 2:
		sub_InitialDraw(wk);
		sys_VBlankFuncChange(sub_VBlank,wk);

		wk->sub_seq = 0;
		return TRUE;
	}
	wk->sub_seq++;
	return FALSE;
}

/**
 *	@brief	アプリケーション解放
 *
 *	@retval	TRUE	解放終了
 *	@retval FALSE	解放中
 */
BOOL SafariLSeq_AppEnd(SAFARIL_WORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		sys_VBlankFuncChange(NULL, NULL);
		sys_HBlankIntrStop();	//HBlank割り込み停止

		sub_DeleteActor(wk);
		sub_ActResRelease(wk);
		sub_SelectListRelease(wk);
		sub_BmpWinDelete(wk);
		sub_MsgDataRelease(wk);
		sub_2DResRelease(wk);
		sub_BGLRelease(wk);
		break;
	case 2:
		//タッチパネル終了
		StopTP();

		GX_SetVisiblePlane(0);
		GXS_SetVisiblePlane(0);
		GF_Disp_GX_VisibleControlInit();
		GF_Disp_GXS_VisibleControlInit();

		wk->sub_seq = 0;
		return TRUE;
	}
	wk->sub_seq++;
	return FALSE;
}

/**
 *	@brief	ブロック選択画面　キー取得
 */
int SafariLBlock_Input(SAFARIL_WORK* wk)
{
	int ret;

	if(input_change(wk)){
		return LSEQ_NULL;
	}
	if(wk->key_mode == APP_KTST_KEY){
		ret = keyin_Block(wk);
	}else{
		ret = touchin_Block(wk);
	}
	return ret;
}

/**
 *	@brief	ブロック選択　リスト選択
 */
int SafariLBlock_ListSelect(SAFARIL_WORK* wk)
{
	int key_mode;
	u32 ret;

	ret = SelectBoxMain(wk->list_wk);
	if(ret == SBOX_SELECT_NULL){
		return LSEQ_LIST_SEL;
	}
	key_mode = wk->list_wk->key_mode;
	SelectBoxExit(wk->list_wk);

	if(key_mode != wk->key_mode){
		if(key_mode == APP_KTST_KEY){
			imodechg_to_key(wk);
		}else{
			imodechg_to_touch(wk);
		}
	}
	wk->key_mode = key_mode;

	switch(ret){
	case 0:	//とりかえる
		wk->c_pos = 0;
		return LSEQ_MODE_SHIFT;
	case 1:	//ならべかえ
		sub_ScrnFilterSet(wk,MFRM_FILTER,0,FALSE);
		sub_ScrnFilterSet(wk,SFRM_FILTER,0,FALSE);

		//カーソルセット
		wk->c_pos = wk->pos;
		sub_SetViewToSortFromBlock(wk);

		wk->next_seq = SEQ_MODE_SORT;
		return LSEQ_END;
	case 2:	//やめる
	default:
		sub_ScrnFilterSet(wk,MFRM_FILTER,0,FALSE);
		sub_ScrnFilterSet(wk,SFRM_FILTER,0,FALSE);
		break;
	}
	return LSEQ_NULL;
}

/**
 *	@brief	ブロック選択⇔取替え画面　移行エフェクト
 */
int SafariLEff_ModeBCShift(SAFARIL_WORK* wk)
{
	int i;

	switch(wk->sub_seq){
	case 0:
		eff_BCScrollInit(wk);
		wk->sub_seq++;
		break;
	case 1:
		if(!eff_BCScroll(wk)){
			break;
		}
		wk->sub_seq++;
		break;
	default:
		//BTS1251 OBJ回り込み防止解除
		GXS_SetVisibleWnd( GX_WNDMASK_NONE );
		wk->scr_ct = 0;
		wk->sub_seq = 0;
		if(wk->mode == MODE_BLOCK){
			wk->next_seq = SEQ_MODE_CHANGE;
			sub_ExpWinDraw(wk,WIN_EXP,MSGIDX_EXP_CHANGE,TRUE);
			sub_BlockInfoWinChangeDraw(wk);
			wk->mode = MODE_CHANGE;
		}else{
			wk->next_seq = SEQ_MODE_BLOCK;
			sub_ExpWinDraw(wk,WIN_EXP,MSGIDX_EXP_DO,TRUE);
			sub_BlockInfoWinDraw(wk,wk->pos);
			wk->mode = MODE_BLOCK;
		}
		return LSEQ_END;
	}
	return LSEQ_MODE_SHIFT;
}

/**
 *	@brief	ブロックチェンジ　入力取得
 */
int SafariLChange_Input(SAFARIL_WORK* wk)
{
	int ret;

	if(input_change(wk)){
		return LSEQ_NULL;
	}
	if(wk->key_mode == APP_KTST_KEY){
		ret = keyin_Change(wk);
	}else{
		ret = touchin_Change(wk);
	}
	return ret;
}

/**
 *	@brief	ブロックチェンジ　リスト選択
 */
int SafariLChange_ListSelect(SAFARIL_WORK* wk)
{
	int key_mode;
	u8 panel;
	u32 ret;

	ret = TOUCH_SW_Main( wk->ynbtn_wk );
	switch(ret){
	case TOUCH_SW_RET_YES:
		ret = TRUE;
		panel = wk->page*PANEL_VIEW_MAX+wk->c_pos;
		if(panel == wk->layout.block[wk->pos].map_id){
			break;	//キャンセル扱い
		}
		//とりかえる
		wk->custom_f = TRUE;
		SafariLayout_BlockReplace(&wk->layout,wk->pos,panel);
		break;
	case TOUCH_SW_RET_NO:
		//元に戻す
		sub_MakePanelCharBuffer(wk,wk->pos,wk->layout.block[wk->pos].map_id);
		ret = FALSE;
		break;
	default:
		return LSEQ_LIST_SEL;
	}
	//現在の操作モードを取得
	key_mode = TOUCH_SW_GetKTStatus(wk->ynbtn_wk);
	if(key_mode != wk->key_mode){
		if(key_mode == APP_KTST_KEY){
			imodechg_to_key(wk);
		}else{
			imodechg_to_touch(wk);
		}
	}
	wk->key_mode = key_mode;
	TOUCH_SW_Reset( wk->ynbtn_wk);

	//ウィンドウ再描画
	GF_BGL_BmpWinOff(&wk->win[WIN_WARN]);
	sub_ExpWinFrameDraw(wk,0);
	sub_ExpWinDraw(wk,WIN_EXP,MSGIDX_EXP_CHANGE,TRUE);

	sub_ScrnFilterSet(wk,MFRM_FILTER,0,FALSE);

	if(ret){
		return LSEQ_MODE_SHIFT;
	}
	return LSEQ_NULL;
}

/**
 *	@brief	ブロックチェンジ　ページスクロール
 */
int SafariLChange_PageScroll(SAFARIL_WORK* wk)
{
	u32 ret;

	switch(wk->sub_seq){
	case 0:
		//次のページを用意
		eff_PageScrollInit(wk);
		wk->sub_seq++;
		break;
	case 1:
		if(!eff_PageScroll(wk)){
			break;
		}
		wk->sub_seq++;
		break;
	default:
		wk->panel_sw ^= 1;
		wk->sub_seq = 0;
		return LSEQ_NULL;
	}
	return LSEQ_EFF;
}

/**
 *	@brief	ブロックソート　入力取得
 */
int SafariLSort_Input(SAFARIL_WORK* wk)
{
	int ret;

	if(input_change(wk)){
		return LSEQ_NULL;
	}
	if(wk->key_mode == APP_KTST_KEY){
		ret = keyin_Sort(wk);
	}else{
		ret = touchin_Sort(wk);
	}
	return ret;
}

/**
 *	@brief	ブロックソート　パネルチェンジ
 */
int SafariLSort_PanelChange(SAFARIL_WORK* wk)
{
	int idx;

	idx = wk->panel_sw*PANEL_VIEW_MAX;
	switch(wk->sub_seq){
	case 0:	//初期化
		CATS_ObjectBGPriSetCap(wk->pAPanel[wk->pos+idx],0);
		CATS_ObjectBGPriSetCap(wk->pAPanel[wk->c_pos+idx],0);
		GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_OFF);
		wk->sub_seq++;
		break;
	case 1:	//スクロールアウト
		if(!eff_PanelChange(wk,0)){
			break;
		}
		//ソート処理
		wk->custom_f = TRUE;
		SafariLayout_BlockSwap(&wk->layout,wk->pos,wk->c_pos);
		//リソース読み替え
		SafariLPanel_ResChange(wk->panel,wk->pos+idx,wk->layout.block[wk->pos].map_id);
		sub_MakePanelCharBuffer(wk,wk->pos,wk->layout.block[wk->pos].map_id);
		SafariLPanel_ResChange(wk->panel,wk->c_pos+idx,wk->layout.block[wk->c_pos].map_id);
		sub_MakePanelCharBuffer(wk,wk->c_pos,wk->layout.block[wk->c_pos].map_id);
		wk->sub_seq++;
		break;
	case 2:	//スクロールイン
		if(!eff_PanelChange(wk,1)){
			break;
		}
		CATS_ObjectBGPriSetCap(wk->pAPanel[wk->pos+idx],PANEL1_PRI);
		CATS_ObjectBGPriSetCap(wk->pAPanel[wk->c_pos+idx],PANEL1_PRI);
		GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON);
		wk->sub_seq++;
		break;
	default:
		wk->pos = wk->c_pos;
		sub_SetViewToBlockFromSort(wk);
		wk->sub_seq = 0;
		wk->next_seq = SEQ_MODE_BLOCK;
		return LSEQ_END;
	}
	return LSEQ_EFF;
}

//----------------------------------------------------------------
///ローカルエリア
//----------------------------------------------------------------

/**
 *	@brief	キー操作モードへ描画変更
 */
static void imodechg_to_key(SAFARIL_WORK* wk)
{
	switch(wk->mode){
	case MODE_BLOCK:
		sub_CursorPosSet(wk,NACT_CURSOR,wk->pos);
		break;
	case MODE_CHANGE:
		sub_CursorPosSet(wk,NACT_CURSOR_C,wk->c_pos);
		sub_CursorPosSet(wk,NACT_CURSOR_S,wk->pos);
		break;
	case MODE_SORT:
		sub_CursorPosSet(wk,NACT_CURSOR,wk->c_pos);
		break;
	}
}

/**
 *	@brief	タッチ操作モードへ描画変更
 */
static void imodechg_to_touch(SAFARIL_WORK* wk)
{
	imodechg_to_key(wk);
	return;
#if 0
	switch(wk->mode){
	case MODE_BLOCK:
		sub_CursorVisibleSet(wk,NACT_CURSOR,wk->pos,FALSE);
		break;
	case MODE_CHANGE:
		sub_CursorVisibleSet(wk,NACT_CURSOR_C,wk->c_pos,FALSE);
		break;
	case MODE_SORT:
		sub_CursorVisibleSet(wk,NACT_CURSOR,0,FALSE);
		break;
	}
	sub_CursorVisibleSet(wk,NACT_CURSOR_S,wk->pos,FALSE);
#endif
}

/*
 *	@brief	キー/タッチ　切替監視
 */
static BOOL input_change(SAFARIL_WORK* wk)
{
	if(wk->key_mode == APP_KTST_TOUCH){
		if(GF_TP_GetCont() != 0){	//
			return FALSE;
		}
		if(sys.cont != 0){
			if(wk->to_key_func != NULL){
				(wk->to_key_func)(wk);
			}
			imodechg_to_key(wk);
			wk->key_mode = APP_KTST_KEY;
			return FALSE;
		}
	}else{
		if(sys.cont != 0){
			return FALSE;
		}
		if(GF_TP_GetCont() != 0){
			if(wk->to_touch_func != NULL){
				(wk->to_touch_func)(wk);
			}
			imodechg_to_touch(wk);
			wk->key_mode = APP_KTST_TOUCH;
			return FALSE;
		}
	}
	return FALSE;
}

/*
 *	@brief	キー取得ブロック　キー
 */
static int keyin_Block(SAFARIL_WORK* wk)
{
	u8 px,py,in;

	if(sys.trg & PAD_BUTTON_CANCEL){
		Snd_SePlay(SE_SAFARIL_CANCEL);
		wk->next_seq = SEQ_FADEOUT;
		return LSEQ_END;
	}
	if(sys.trg & PAD_BUTTON_DECIDE){
		if(wk->pos >= PANEL_VIEW_MAX){
			Snd_SePlay(SE_SAFARIL_CANCEL);
			wk->next_seq = SEQ_FADEOUT;
			return LSEQ_END;
		}
		//選択リスト描画
		Snd_SePlay(SE_SAFARIL_DECIDE);
		sub_SBoxBlockSet(wk);
		return LSEQ_LIST_SEL;
	}

	if(!(sys.trg & PAD_KEY_ALL)){
		return LSEQ_NULL;
	}
	px = wk->pos % 3;
	py = wk->pos / 3;
	in = 0;

#if GS_AFTERMASTER_GF1058_20091008_FIX
  if(py < 2){
  	if(sys.trg & PAD_KEY_RIGHT){
  		px = (px+1)%3;
  		in = TRUE;
  	}else if(sys.trg & PAD_KEY_LEFT){
  		px = (px+2)%3;
  		in = TRUE;
    }
	}
  if(sys.trg & PAD_KEY_UP){
#else
	if(sys.trg & PAD_KEY_RIGHT){
		px = (px+1)%3;
		in = TRUE;
	}else if(sys.trg & PAD_KEY_LEFT){
		px = (px+2)%3;
		in = TRUE;
	}else if(sys.trg & PAD_KEY_UP){
#endif
		py = (py+2)%3;
		in = TRUE;
	}else if(sys.trg & PAD_KEY_DOWN){
		py = (py+1)%3;
		in = TRUE;
	}
	wk->pos = py*3+px;
#if GS_BTS5566_20090715_FIX
	if((py < 2) || (sys.trg & (PAD_KEY_UP|PAD_KEY_DOWN))){
		Snd_SePlay(SE_SAFARIL_SELECT);
	}
#else
	Snd_SePlay(SE_SAFARIL_SELECT);
#endif
	sub_CursorPosSet(wk,NACT_CURSOR,wk->pos);
	sub_BlockInfoWinDraw(wk,wk->pos);

	return LSEQ_NULL;
}
static int touchin_Block(SAFARIL_WORK* wk)
{
	int ret;
	u16	pat;

	ret = GF_TP_RectHitTrg(Panel1_TpRect);
	if(	ret == RECT_HIT_NONE){
		return LSEQ_NULL;
	}
	if(ret == PANEL_VIEW_MAX){
		pat = 0xFFFE;	//1111 1111 1111 1110	無効パレットbit(onになっている色Noのみ反応)
		if(GF_BGL_DotCheck(wk->bgl,MFRM_BACK,sys.tp_x,sys.tp_y,&pat) == FALSE){
			return LSEQ_NULL;
		}
		Snd_SePlay(SE_SAFARIL_CANCEL);
		wk->next_seq = SEQ_FADEOUT;
		return LSEQ_END;
	}
	//選択リスト描画
	wk->pos = ret;
	wk->c_pos = 0;
	sub_CursorPosSet(wk,NACT_CURSOR,wk->pos);
	sub_BlockInfoWinDraw(wk,wk->pos);
	Snd_SePlay(SE_SAFARIL_DECIDE);
	sub_SBoxBlockSet(wk);
	return LSEQ_LIST_SEL;
}

/**
 *	@brief	入力取得チェンジ　入れ替え決定
 */
static int exe_ChangePanel(SAFARIL_WORK* wk)
{
	Snd_SePlay(SE_SAFARIL_DECIDE);

	if(wk->obj_place_f){
		//配置物をクリアしていいか確認シーケンスに飛ぶ
		sub_SBoxChangeSet(wk);
		return LSEQ_LIST_SEL;
	}else{	//いきなりとりかえる
		u8 panel = wk->page*PANEL_VIEW_MAX+wk->c_pos;
		wk->custom_f = TRUE;
		SafariLayout_BlockReplace(&wk->layout,wk->pos,panel);
#if GS_BTS5543_20090710_FIX
		sub_MakePanelCharBuffer(wk,wk->pos,wk->layout.block[wk->pos].map_id);
#endif
		sub_ScrnFilterSet(wk,MFRM_FILTER,0,FALSE);
		return LSEQ_MODE_SHIFT;
	}
}

/*
 *	@brief	入力取得チェンジ　キー
 */
static int keyin_Change(SAFARIL_WORK* wk)
{
	u8 px,py,in;
#if	GS_BTS5566_20090715_FIX
  u8  old_cpos = wk->c_pos;
#endif

	if(sys.trg & PAD_BUTTON_CANCEL){
		Snd_SePlay(SE_SAFARIL_CANCEL);
		return LSEQ_MODE_SHIFT;
	}
	if(sys.trg & PAD_BUTTON_DECIDE){
		if(wk->c_pos >= PANEL_VIEW_MAX){
			Snd_SePlay(SE_SAFARIL_CANCEL);
			return LSEQ_MODE_SHIFT;
		}
		if((wk->page*PANEL_VIEW_MAX+wk->c_pos) == wk->layout.block[wk->pos].map_id){
			return LSEQ_NULL;	//なにもしない
		}
		//選択リスト描画
		return exe_ChangePanel(wk);
	}

	if(!(sys.trg & PAD_KEY_ALL)){
		return LSEQ_NULL;
	}
	px = wk->c_pos % 3;
	py = wk->c_pos / 3;
	in = 0;

#if GS_AFTERMASTER_GF1058_20091008_FIX
	if(sys.trg & PAD_KEY_RIGHT && py < 2){
#else
	if(sys.trg & PAD_KEY_RIGHT){
#endif
		if(px == 2){
			if(py == 0){
				py ^= 1;
				px = (px+1)%3;
			}else if(py < 2 && wk->page < (PAGE_MAX-1)){
				in = TRUE;
				wk->scrl_d = SCRL_LEFT;
				py ^= 1;
				px = (px+1)%3;
			}
		}else{
			px = (px+1)%3;
		}
#if GS_AFTERMASTER_GF1058_20091008_FIX
  }else if(sys.trg & PAD_KEY_LEFT && py < 2){
#else
	}else if(sys.trg & PAD_KEY_LEFT){
#endif
		if(px == 0){
			if(py == 1){
				py ^= 1;
				px = (px+2)%3;
			}else if(py < 2 && wk->page > 0){
				in = TRUE;
				wk->scrl_d = SCRL_RIGHT;
				py ^= 1;
				px = (px+2)%3;
			}
		}else{
			px = (px+2)%3;
		}
	}else if(sys.trg & PAD_KEY_UP){
		py = (py+2)%3;
	}else if(sys.trg & PAD_KEY_DOWN){
		py = (py+1)%3;
	}
	wk->c_pos = py*3+px;
#if	GS_BTS5571_20090716_FIX
#else
	sub_BlockInfoWinChangeDraw(wk);
#endif
	sub_CursorPosSet(wk,NACT_CURSOR_C,wk->c_pos);

	if(in){
		Snd_SePlay(SE_SAFARIL_PAGE_CHG);
		sub_CursorVisibleSet(wk,NACT_CURSOR_C,0,FALSE);
		return LSEQ_EFF;
	}else{
#if	GS_BTS5566_20090715_FIX
    if(old_cpos != wk->c_pos){
  	 	Snd_SePlay(SE_SAFARIL_SELECT);
		}
#else
		Snd_SePlay(SE_SAFARIL_SELECT);
#endif
	}
#if	GS_BTS5571_20090716_FIX
	sub_BlockInfoWinChangeDraw(wk);
#endif
	return LSEQ_NULL;
}
static int touchin_Change(SAFARIL_WORK* wk)
{
	int ret;
	u16	pat;

	ret = GF_TP_RectHitTrg(Panel2_TpRect);
	if(	ret == RECT_HIT_NONE){
		return LSEQ_NULL;
	}
	switch(ret){
	case PANEL_VIEW_MAX:
		pat = 0xFFFE;	//1111 1111 1111 1110	無効パレットbit(onになっている色Noのみ反応)
		if(GF_BGL_DotCheck(wk->bgl,MFRM_BACK,sys.tp_x,sys.tp_y,&pat) == FALSE){
			return LSEQ_NULL;
		}
		wk->c_pos = PANEL_VIEW_MAX;
		Snd_SePlay(SE_SAFARIL_CANCEL);
		return LSEQ_MODE_SHIFT;
	case (PANEL_VIEW_MAX+1):
		if(wk->page == 0){
			return LSEQ_NULL;
		}
		wk->scrl_d = SCRL_RIGHT;
		wk->c_pos = PANEL_VIEW_MAX-1;
		sub_CursorVisibleSet(wk,NACT_CURSOR_C,0,FALSE);
		CLACT_SetAnmFlag(wk->pAct[NACT_ALLOW_L],TRUE);
		CLACT_AnmReStart(wk->pAct[NACT_ALLOW_L]);
		Snd_SePlay(SE_SAFARIL_PAGE_CHG);
		return LSEQ_EFF;
	case (PANEL_VIEW_MAX+2):
		if(wk->page == (PAGE_MAX-1)){
			return LSEQ_NULL;
		}
		wk->scrl_d = SCRL_LEFT;
		wk->c_pos = 0;
		sub_CursorVisibleSet(wk,NACT_CURSOR_C,0,FALSE);
		CLACT_SetAnmFlag(wk->pAct[NACT_ALLOW_R],TRUE);
		CLACT_AnmReStart(wk->pAct[NACT_ALLOW_R]);
		Snd_SePlay(SE_SAFARIL_PAGE_CHG);
		return LSEQ_EFF;
	}
	//選択リスト描画
	wk->c_pos = ret;
	sub_CursorPosSet(wk,NACT_CURSOR_C,wk->c_pos);
	sub_BlockInfoWinChangeDraw(wk);

	if((wk->page*PANEL_VIEW_MAX+wk->c_pos) == wk->layout.block[wk->pos].map_id){
		return LSEQ_NULL;	//なにもしない
	}

	return exe_ChangePanel(wk);
#if 0
	Snd_SePlay(SE_SAFARIL_DECIDE);
	sub_SBoxChangeSet(wk);
	return LSEQ_LIST_SEL;
#endif
}

/**
 *	@brief	ソートモードへ移行
 */
static void sub_SetViewToSortFromBlock(SAFARIL_WORK* wk)
{
	wk->mode = MODE_SORT;

//	if(wk->key_mode == APP_KTST_KEY){
		sub_CursorPosSet(wk,NACT_CURSOR,wk->c_pos);
//	}
	CATS_ObjectBGPriSet(wk->pAct[NACT_CURSOR],0);
	sub_CursorPosSet(wk,NACT_CURSOR_A,wk->pos);
	sub_ExpWinDraw(wk,WIN_EXP,MSGIDX_EXP_SORT,TRUE);
}

/**
 *	@brief	ブロック選択モードへ移行
 */
static void sub_SetViewToBlockFromSort(SAFARIL_WORK* wk)
{
	wk->mode = MODE_BLOCK;
//	if(wk->key_mode == APP_KTST_KEY){
		sub_CursorPosSet(wk,NACT_CURSOR,wk->pos);
//	}
	CATS_ObjectBGPriSet(wk->pAct[NACT_CURSOR],PANEL1_PRI);
	sub_CursorVisibleSet(wk,NACT_CURSOR_A,0,FALSE);
	sub_ExpWinDraw(wk,WIN_EXP,MSGIDX_EXP_DO,TRUE);
}

/*
 *	@brief	入力取得ソート　キー
 */
static int keyin_Sort(SAFARIL_WORK* wk)
{
	u8 px,py,in;

	if(sys.trg & PAD_BUTTON_CANCEL){
		sub_SetViewToBlockFromSort(wk);
		wk->next_seq = SEQ_MODE_BLOCK;
		Snd_SePlay(SE_SAFARIL_CANCEL);
		return LSEQ_END;
	}
	if(sys.trg & PAD_BUTTON_DECIDE){
		if(	wk->c_pos >= PANEL_VIEW_MAX ||
			wk->pos == wk->c_pos){
			sub_SetViewToBlockFromSort(wk);
			wk->next_seq = SEQ_MODE_BLOCK;
			Snd_SePlay(SE_SAFARIL_CANCEL);
			return LSEQ_END;
		}
		//並べ替えエフェクトへ
		Snd_SePlay(SE_SAFARIL_SLIDE);
		return LSEQ_EFF;
	}

	if(!(sys.trg & PAD_KEY_ALL)){
		return LSEQ_NULL;
	}
	px = wk->c_pos % 3;
	py = wk->c_pos / 3;
	in = 0;

#if GS_AFTERMASTER_GF1058_20091008_FIX
  if(py < 2){
	  if(sys.trg & PAD_KEY_RIGHT){
	  	px = (px+1)%3;
		  in = TRUE;
	  }else if(sys.trg & PAD_KEY_LEFT){
		  px = (px+2)%3;
		  in = TRUE;
    }
	}
  if(sys.trg & PAD_KEY_UP){
#else
	if(sys.trg & PAD_KEY_RIGHT){
		px = (px+1)%3;
		in = TRUE;
	}else if(sys.trg & PAD_KEY_LEFT){
		px = (px+2)%3;
		in = TRUE;
	}else if(sys.trg & PAD_KEY_UP){
#endif
		py = (py+2)%3;
		in = TRUE;
	}else if(sys.trg & PAD_KEY_DOWN){
		py = (py+1)%3;
		in = TRUE;
	}
	wk->c_pos = py*3+px;
#if GS_BTS5566_20090715_FIX
	if((py < 2) || (sys.trg & (PAD_KEY_UP|PAD_KEY_DOWN))){
		Snd_SePlay(SE_SAFARIL_SELECT);
	}
#else
	Snd_SePlay(SE_SAFARIL_SELECT);
#endif
	sub_CursorPosSet(wk,NACT_CURSOR,wk->c_pos);

	return LSEQ_NULL;
}
static int touchin_Sort(SAFARIL_WORK* wk)
{
	int ret;
	u16	pat;

	ret = GF_TP_RectHitTrg(Panel1_TpRect);
	if(	ret == RECT_HIT_NONE){
		return LSEQ_NULL;
	}
	if(ret == PANEL_VIEW_MAX){
		pat = 0xFFFE;	//1111 1111 1111 1110	無効パレットbit(onになっている色Noのみ反応)
		if(GF_BGL_DotCheck(wk->bgl,MFRM_BACK,sys.tp_x,sys.tp_y,&pat) == FALSE){
			return LSEQ_NULL;
		}
		wk->c_pos = PANEL_VIEW_MAX;
		sub_SetViewToBlockFromSort(wk);
		wk->next_seq = SEQ_MODE_BLOCK;
		Snd_SePlay(SE_SAFARIL_CANCEL);
		return LSEQ_END;
	}
	//選択リスト描画
	if(wk->pos == ret){
		sub_SetViewToBlockFromSort(wk);
		wk->next_seq = SEQ_MODE_BLOCK;
		Snd_SePlay(SE_SAFARIL_CANCEL);
		return LSEQ_END;
	}
	wk->c_pos = ret;
	sub_CursorPosSet(wk,NACT_CURSOR,wk->c_pos);
	//並べ替えエフェクトへ
	Snd_SePlay(SE_SAFARIL_SLIDE);
	return LSEQ_EFF;
}

/*
 *	@brief	ブロック選択画面　選択ボックス表示開始
 */
static void sub_SBoxBlockSet(SAFARIL_WORK* wk)
{
	SELBOX_HEADER head;

	MI_CpuClear8(&head,sizeof(SELBOX_HEADER));

	head.prm = data_sbox_sel;
	head.list = (const BMPLIST_DATA*)wk->slist;
	head.bgl = wk->bgl;

	head.count = SBOX_SEL_CT;

	wk->list_wk = SelectBoxSet(wk->sbox,&(head),wk->key_mode,
			data_sbox_pos[wk->pos].x,data_sbox_pos[wk->pos].y,0,0);

	sub_ScrnFilterSet(wk,MFRM_FILTER,wk->pos,TRUE);
	sub_ScrnFilterSet(wk,SFRM_FILTER,wk->pos,TRUE);
}

/*
 *	@brief	ブロックチェンジ画面　選択ボックス表示開始
 */
static void sub_SBoxChangeSet(SAFARIL_WORK* wk)
{
	TOUCH_SW_PARAM param;
	MI_CpuClear8(&param,sizeof(TOUCH_SW_PARAM));

	param.p_bgl  = wk->bgl;
	param.bg_frame = BMPL_YESNO_FRM;
	param.char_offs = BMPL_YESNO_CGX;
	param.pltt_offs = BMPL_YESNO_PAL;
	param.x		= BMPL_YESNO_PX;
	param.y		= BMPL_YESNO_PY;
	param.kt_st = wk->key_mode;
	param.key_pos = 0;
	TOUCH_SW_Init( wk->ynbtn_wk, &param);

	sub_ScrnFilterSet(wk,MFRM_FILTER,wk->c_pos,TRUE);
	sub_MakePanelCharBuffer(wk,wk->pos,wk->page*PANEL_VIEW_MAX+wk->c_pos);

	//ウィンドウ描画
	sub_ExpWinFrameDraw(wk,1);
	sub_ExpWinDraw(wk,WIN_WARN,MSGIDX_EXP_WARN,TRUE);
}

/**
 *	@brief	サファリレイアウト VBlank関数
 */
static void sub_VBlank(void* work)
{
	SAFARIL_WORK* wk = (SAFARIL_WORK*)work;

	//アクター転送
	SafariL_ActorTrans(wk);

	if(wk->scrl_f){
		GF_BGL_ScrollSet( wk->bgl,MFRM_FILTER, GF_BGL_SCROLL_Y_SET, wk->scrl_m);
		GF_BGL_ScrollSet( wk->bgl,MFRM_BLOCK, GF_BGL_SCROLL_Y_SET, wk->scrl_m);

		GF_BGL_ScrollSet( wk->bgl,SFRM_FILTER, GF_BGL_SCROLL_Y_SET, wk->scrl_s);
		GF_BGL_ScrollSet( wk->bgl,SFRM_BLOCK, GF_BGL_SCROLL_Y_SET, wk->scrl_s);
		GF_BGL_ScrollSet( wk->bgl,SFRM_PANEL, GF_BGL_SCROLL_Y_SET, wk->scrl_s);
		wk->scrl_f = FALSE;
	}

	DoVramTransferManager();
	GF_BGL_VBlankFunc( wk->bgl );
	OS_SetIrqCheckFlag( OS_IE_V_BLANK);
}

/**
 *	@brief	サファリレイアウトVramBank設定
 */
static void sub_VBankSet(void)
{
	GF_BGL_DISPVRAM vramSetTable = {
		GX_VRAM_BG_128_A,				// メイン2DエンジンのBG
		GX_VRAM_BGEXTPLTT_NONE,			// メイン2DエンジンのBG拡張パレット
		GX_VRAM_SUB_BG_128_C,			// サブ2DエンジンのBG
		GX_VRAM_SUB_BGEXTPLTT_NONE,		// サブ2DエンジンのBG拡張パレット
		GX_VRAM_OBJ_64_E,				// メイン2DエンジンのOBJ
		GX_VRAM_OBJEXTPLTT_NONE,		// メイン2DエンジンのOBJ拡張パレット
		GX_VRAM_SUB_OBJ_16_I,			// サブ2DエンジンのOBJ
		GX_VRAM_SUB_OBJEXTPLTT_NONE,	// サブ2DエンジンのOBJ拡張パレット
		GX_VRAM_TEX_NONE,				// テクスチャイメージスロット
		GX_VRAM_TEXPLTT_NONE			// テクスチャパレットスロット
	};
	GF_Disp_SetBank( &vramSetTable );
}


/**
 *	@brief	BGL初期化
 */
static void sub_BGLInit(SAFARIL_WORK* wk)
{
	int i,frame;
	GF_BGL_INI* bgl;

	//VramBankセット
	sub_VBankSet();

	//上画面をサブ,下画面をメインに切り替え
	GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);

	//BGL初期化
	wk->bgl = GF_BGL_BglIniAlloc(wk->heapID);

	{	//BG SYSTEM
		GF_BGL_SYS_HEADER BGsys_data = {
			GX_DISPMODE_GRAPHICS,GX_BGMODE_0,GX_BGMODE_0,GX_BG0_AS_2D
		};
		GF_BGL_InitBG(&BGsys_data);
	}

	{
	GF_BGL_BGCNT_HEADER TextBgCntDat[] = {
		{	//MAIN BG0
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			0,0,0,FALSE},
		{	//MAIN BG1
			0,0,0x1000,0,GF_BGL_SCRSIZ_256x512,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			1,0,0,FALSE},
		{	//MAIN BG2
			0,0,0x1000,0,GF_BGL_SCRSIZ_256x512,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xd000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			2,0,0,FALSE},
		{	//MAIN BG3
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			3,0,0,FALSE},
		{	//SUB BG0
			0,0,0x1000,0,GF_BGL_SCRSIZ_256x512,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			0,0,0,FALSE},
		{	//SUB BG1
			0,0,0x1000,0,GF_BGL_SCRSIZ_256x512,GX_BG_COLORMODE_256,
			GX_BG_SCRBASE_0xc000,GX_BG_CHARBASE_0x10000,GX_BG_EXTPLTT_01,
			1,0,0,FALSE},
		{	//SUB BG2
			0,0,0x1000,0,GF_BGL_SCRSIZ_256x512,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xd000,GX_BG_CHARBASE_0x04000,GX_BG_EXTPLTT_01,
			2,0,0,FALSE},
		{	//SUB BG3
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000,GX_BG_CHARBASE_0x04000,GX_BG_EXTPLTT_01,
			3,0,0,FALSE},
	};
		frame = GF_BGL_FRAME0_M;
		for(i = 0;i < 8;i++){
			GF_BGL_BGControlSet(wk->bgl,frame,&(TextBgCntDat[i]),GF_BGL_MODE_TEXT);
			GF_BGL_ScrClear(wk->bgl,frame);
			GF_BGL_ClearCharSet(frame,64,0,wk->heapID);
			++frame;
		}
	}
	G2_SetBlendAlpha(ALPHA_MASK_1ST,ALPHA_MASK_2ND,ALPHA_SECOND,ALPHA_FIRST);
	G2S_SetBlendAlpha(ALPHA_MASKS_1ST,ALPHA_MASKS_2ND,ALPHA_SECOND,ALPHA_FIRST);
}

/**
 *	@brief	BGL解放
 */
static void sub_BGLRelease(SAFARIL_WORK* wk)
{
	int i;

	G2_SetBlendAlpha(GX_BLEND_PLANEMASK_NONE,GX_BLEND_PLANEMASK_NONE,31,0);
	G2S_SetBlendAlpha(GX_BLEND_PLANEMASK_NONE,GX_BLEND_PLANEMASK_NONE,31,0);

	for(i = 0; i <  8;i++){
		GF_BGL_BGControlExit( wk->bgl, i);
	}
	sys_FreeMemoryEz(wk->bgl);

	//上画面をメイン,下画面をサブに戻す
	GX_SetDispSelect(GX_DISP_SELECT_MAIN_SUB);
}

/**
 *	@brief	2DBGリソース初期化
 */
static void sub_2DResInit(SAFARIL_WORK* wk)
{
	ARCHANDLE* handle;

	//アーカイブハンドル取得
	handle = ArchiveDataHandleOpen(ARC_SAFARIL_GRA,wk->heapID);

	//キャラクタ転送
	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgd_back_ncgr,
		MFRM_BACK,0,0,0);
	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgd_back_ncgr,
		SFRM_FILTER,0,0,0);

	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgu_back_ncgr,
		SFRM_BACK,0,0,0);

	//パレット転送
	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgd_nclr,
		GF_BGL_FRAME3_M,2,0,0);
	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgu_nclr,
		GF_BGL_FRAME3_S,2,0,0);

	//スクリーン転送
	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgd_bk02_nscr,
		MFRM_BLOCK,1,0,0);
	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgd_back_nscr,
		MFRM_BACK,1,0,0);
	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgu_bk02_nscr,
		SFRM_BLOCK,1,0,0);

	APP_ArcFileVramLoad(wk->bgl,wk->heapID,
		handle,ARC_SAFARIL_GRA,NARC_safaril_gra_bgu_back_nscr+(!wk->obj_place_f),
		SFRM_BACK,1,0,0);

	//スクリーンリソース取得
	wk->pSBufWin = ArcUtil_HDL_ScrnDataGet(handle,
			NARC_safaril_gra_bgd_bk00_nscr,FALSE,
			&wk->pSWin,wk->heapID);
	wk->pSBufFl = ArcUtil_HDL_ScrnDataGet(handle,
			NARC_safaril_gra_bgd_bk01_nscr,FALSE,
			&wk->pSFl,wk->heapID);

	//ハンドル閉じる
	ArchiveDataHandleClose( handle );

	sub_MakePanelScreen(wk);
	sub_MakePanelCharBuffer(wk,0xFF,0);

	//スクリーン転送
	GF_BGL_LoadScreenV_Req(wk->bgl,MFRM_MSG);
	GF_BGL_LoadScreenV_Req(wk->bgl,MFRM_BLOCK);
	GF_BGL_LoadScreenV_Req(wk->bgl,MFRM_BACK);
	GF_BGL_LoadScreenV_Req(wk->bgl,SFRM_BLOCK);
	GF_BGL_LoadScreenV_Req(wk->bgl,SFRM_BACK);
}

/**
 *	@brief	2DBGリソース解放
 */
static void sub_2DResRelease(SAFARIL_WORK* wk)
{
	sys_FreeMemoryEz(wk->pSBufWin);
	sys_FreeMemoryEz(wk->pSBufFl);
}

/**
 *	@brief	メッセージデータ初期化
 */
static void sub_MsgDataInit(SAFARIL_WORK* wk)
{
	int i = 0;

	//ここでボタンフォントも追加しておく
	FontProc_LoadFont( FONT_TOUCH, wk->heapID );

	wk->msg.man = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,
			NARC_msg_safari_l_dat,wk->heapID);

	wk->msg.wset = WORDSET_CreateEx(1,WORDSET_BUFLEN,wk->heapID);
	wk->msg.tmp = STRBUF_Create(SAFARIL_BUFLEN,wk->heapID);

	wk->msg.page = MSGMAN_AllocString(wk->msg.man,msg_safaril_page);
	wk->msg.obj_num = MSGMAN_AllocString(wk->msg.man,msg_safaril_info_num);
	for(i = 0;i < MSGIDX_SEL_MAX;i++){
		wk->msg.sel[i] = MSGMAN_AllocString(wk->msg.man,msg_safaril_sel_back+i);
	}
	for(i = 0;i < MSGIDX_EXP_MAX;i++){
		wk->msg.exp[i] = MSGMAN_AllocString(wk->msg.man,msg_safaril_exp01+i);
	}
}

/**
 *	@brief	メッセージデータ解放
 */
static void sub_MsgDataRelease(SAFARIL_WORK* wk)
{
	int i = 0;

	for(i = 0;i < MSGIDX_EXP_MAX;i++){
		STRBUF_Delete(wk->msg.exp[i]);
	}
	for(i = 0;i < MSGIDX_SEL_MAX;i++){
		STRBUF_Delete(wk->msg.sel[i]);
	}
	STRBUF_Delete(wk->msg.obj_num);
	STRBUF_Delete(wk->msg.page);
	STRBUF_Delete(wk->msg.tmp);

	WORDSET_Delete(wk->msg.wset);
	MSGMAN_Delete(wk->msg.man);

	FontProc_UnloadFont( FONT_TOUCH );
}

/**
 *	@brief	BMPウィンドウ初期化
 */
static void sub_BmpWinAdd(SAFARIL_WORK* wk)
{
	int i,idx,cgx;
	u8 sy,py;
	static const BMPWIN_DAT windat[] = {
	 { BMPL_EXP_FRM,BMPL_EXP_PX,BMPL_EXP_PY,BMPL_EXP_SX,BMPL_EXP_SY,
	   BMPL_EXP_PAL,BMPL_EXP_CGX},
	 { BMPL_WARN_FRM,BMPL_WARN_PX,BMPL_WARN_PY,BMPL_WARN_SX,BMPL_WARN_SY,
	   BMPL_WARN_PAL,BMPL_WARN_CGX},
	 { BMPL_BACK_FRM,BMPL_BACK_PX,BMPL_BACK_PY,BMPL_BACK_SX,BMPL_BACK_SY,
	   BMPL_BACK_PAL,BMPL_BACK_CGX},
	 { BMPL_PAGE_FRM,BMPL_PAGE_PX,BMPL_PAGE_PY,BMPL_PAGE_SX,BMPL_PAGE_SY,
	   BMPL_PAGE_PAL,BMPL_PAGE_CGX},
	 { BMPL_INFO_FRM,BMPL_INFO_00_PX,BMPL_INFO_00_PY,BMPL_INFO_00_SX,BMPL_INFO_00_SY,
	   BMPL_INFO_PAL,BMPL_INFO_00_CGX},
	};
	for(i = 0;i <= WIN_INFO_00;i++){
		GF_BGL_BmpWinAddEx(wk->bgl,&(wk->win[i]),&windat[i]);
		GF_BGL_BmpWinDataFill(&(wk->win[i]),FBMP_COL_NULL);
	}
	//上画面情報ウィンドウ
	cgx = BMPL_INFO_00_CGX;
	for(i = 0;i < SAFARI_ATTR_MAX;i++){
		u8 flag;
		idx = WIN_INFO_01_01+(i*2);
		py = BMPL_INFO_PY+(i*4);
		flag = (i/4)^1;
		sy = flag*2+2;

		cgx -= BMPL_INFO_SX1*sy;
		GF_BGL_BmpWinAdd( wk->bgl,&(wk->win[idx+0]),BMPL_INFO_FRM,
			BMPL_INFO_PX1, py, BMPL_INFO_SX1, sy, BMPL_INFO_PAL, cgx);
		GF_BGL_BmpWinDataFill(&(wk->win[idx+0]),GetInfoBaseCol(i%2));
		cgx -= BMPL_INFO_SX1*sy;

		GF_BGL_BmpWinAdd( wk->bgl,&(wk->win[idx+1]),BMPL_INFO_FRM,
			BMPL_INFO_PX2, py+flag, BMPL_INFO_SX2, BMPL_INFO_SY2, BMPL_INFO_PAL, cgx);
		GF_BGL_BmpWinDataFill(&(wk->win[idx+1]),GetInfoBaseCol(i%2));
		cgx -= BMPL_INFO_SX2*BMPL_INFO_SY2;
	}
	//FontOam用BMPWinを作成
	GF_BGL_BmpWinInit(&(wk->win_obj));
	GF_BGL_BmpWinObjAdd(wk->bgl,
		&(wk->win_obj),BMPL_OBJ_SX,BMPL_OBJ_SY,BMPL_OBJ_CGX,0);
	GF_BGL_BmpWinObjDataFill(&(wk->win_obj),FBMP_COL_NULL);

	//YesNoボタンシステムワーク確保
	wk->ynbtn_wk = TOUCH_SW_AllocWork(wk->heapID);
}

/**
 *	@brief	BMPウィンドウ解放
 */
static void sub_BmpWinDelete(SAFARIL_WORK* wk)
{
	int i;

	//YesNoボタンシステムワーク解放
	TOUCH_SW_FreeWork( wk->ynbtn_wk);

	GF_BGL_BmpWinDel(&(wk->win_obj));

	for(i = 0;i < WIN_MAX;i++){
		GF_BGL_BmpWinOff(&(wk->win[i]));
		GF_BGL_BmpWinDel(&(wk->win[i]));
	}
}

/**
 *	@brief	選択ボックスリスト初期化
 */
static void sub_SelectListInit(SAFARIL_WORK* wk)
{
	int i;

	//BMPリスト領域作成
	wk->slist = BMP_MENULIST_Create(SBOX_SEL_CT,wk->heapID);
	wk->dclist = BMP_MENULIST_Create(SBOX_DC_CT,wk->heapID);

	BMP_MENULIST_AddArchiveString(wk->slist,wk->msg.man,msg_safaril_sel_change,0);
	BMP_MENULIST_AddArchiveString(wk->slist,wk->msg.man,msg_safaril_sel_replace,1);
	BMP_MENULIST_AddArchiveString(wk->slist,wk->msg.man,msg_safaril_sel_cancel,2);

	BMP_MENULIST_AddArchiveString(wk->dclist,wk->msg.man,msg_safaril_sel_decide,0);
	BMP_MENULIST_AddArchiveString(wk->dclist,wk->msg.man,msg_safaril_sel_cancel,1);
}

/**
 *	@brief	選択ボックスリスト解放
 */
static void sub_SelectListRelease(SAFARIL_WORK* wk)
{
	//リスト解放
	BMP_MENULIST_Delete(wk->dclist);
	BMP_MENULIST_Delete(wk->slist);

	wk->slist = NULL;
	wk->dclist = NULL;
}

/**
 *	@brief	アクターリソース初期化
 */
static void sub_ActResInit(SAFARIL_WORK* wk)
{
	SafariL_ActorSysInit(wk);

	wk->panel = SafariLPanel_Init(wk->heapID,PANEL_ACTMAX,
		NNS_G2D_VRAM_TYPE_2DMAIN,CATS_CGXOFS_OFFSET);

	SafariL_ActorResInit(wk);

	wk->sbox = SelectBoxSys_AllocWork(wk->heapID ,NULL);

	//FontOamマネージャを作成
	wk->pFOamSys = FONTOAM_SysInit(BMPL_OBJ_MAX,wk->heapID);
}

/**
 *	@brief	アクターリソース解放
 */
static void sub_ActResRelease(SAFARIL_WORK* wk)
{
	//FontOamマネージャを削除
	FONTOAM_SysDelete(wk->pFOamSys);

	SelectBoxSys_Free(wk->sbox);

	SafariL_ActorResRelease(wk);
	SafariLPanel_Release(wk->panel);
	SafariL_ActorSysRelease(wk);
}

/**
 *	@brief	アクター作成
 */
static void sub_MakeActor(SAFARIL_WORK* wk)
{
	int i;

	static const TCATS_OBJECT_ADD_PARAM	ActAddParam[] =
	{
		{	// カーソル
			RESACT_NORMAL,	//ID
			0,0,0,	//x,y,z
			ANM_N_CURSOR, 0, SAFARIL_NOAM_PALIDX+0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル S
			RESACT_NORMAL_SUB,	//ID
			0,0,0,	//x,y,z
			ANM_N_CURSOR, 3, SAFARIL_NOAM_PALIDX+0, NNS_G2D_VRAM_TYPE_2DSUB,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// やじるし
			RESACT_NORMAL,	//ID
			ALLOW_L_PX,ALLOW_PY,0,	//x,y,z
			ANM_N_ALLOW_L, 3, SAFARIL_NOAM_PALIDX+0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// やじるし
			RESACT_NORMAL,	//ID
			ALLOW_R_PX,ALLOW_PY,0,	//x,y,z
			ANM_N_ALLOW_R, 3, SAFARIL_NOAM_PALIDX+0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
	};

	for(i = 0;i < PANEL_VIEW_MAX;i++){
		wk->pAPanel[i] = SafariLPanel_ObjAdd(wk->panel,
			PANEL1_PX+((i%3)*PANEL1_OX),
			PANEL1_PY+((i/3)*PANEL1_OY),
			PANEL1_PRI,PANEL1_SPRI,
			i,wk->layout.block[i].map_id,NNS_G2D_VRAM_TYPE_2DMAIN);
	}
	for(i = 0;i < PANEL_VIEW_MAX;i++){
		wk->pAPanel[i+PANEL_VIEW_MAX] = SafariLPanel_ObjAdd(wk->panel,
			PANEL2_PX+((i%3)*PANEL2_OX),
			PANEL2_PY+((i/3)*PANEL2_OY),
			PANEL2_PRI,PANEL2_SPRI,
			i+PANEL_VIEW_MAX,wk->page*PANEL_VIEW_MAX+i,NNS_G2D_VRAM_TYPE_2DMAIN);
	}
	//ノーマルアクター作成
	wk->pAct[NACT_CURSOR] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[0]);
	wk->pAct[NACT_CURSOR_C] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[0]);
	wk->pAct[NACT_CURSOR_A] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[0]);
	wk->pAct[NACT_CURSOR_S] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[1]);
	wk->pAct[NACT_ALLOW_L] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[2]);
	wk->pAct[NACT_ALLOW_R] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[3]);

	CATS_ObjectBGPriSet(wk->pAct[NACT_CURSOR_C], PANEL2_PRI);
	CATS_ObjectBGPriSet(wk->pAct[NACT_ALLOW_L], PANEL2_PRI);
	CATS_ObjectBGPriSet(wk->pAct[NACT_ALLOW_R], PANEL2_PRI);

	CATS_ObjectBGPriSet(wk->pAct[NACT_CURSOR_A], 0);
	CATS_ObjectPriSet(wk->pAct[NACT_CURSOR_A], 5);

	CATS_ObjectEnable(wk->pAct[NACT_CURSOR], FALSE);
	CATS_ObjectEnable(wk->pAct[NACT_CURSOR_C], FALSE);
	CATS_ObjectEnable(wk->pAct[NACT_CURSOR_A], FALSE);
	CATS_ObjectEnable(wk->pAct[NACT_CURSOR_S], FALSE);

	sub_MakeFontOam(wk);
}

/**
 *	@brief	アクター削除
 */
static void sub_DeleteActor(SAFARIL_WORK* wk)
{
	int i;

	sub_DelFontOam(wk);

	CATS_ObjectDel(wk->pAct[NACT_CURSOR]);
	CATS_ObjectDel(wk->pAct[NACT_CURSOR_C]);
	CATS_ObjectDel(wk->pAct[NACT_CURSOR_S]);
	CATS_ObjectDel(wk->pAct[NACT_CURSOR_A]);
	CATS_ObjectDel(wk->pAct[NACT_ALLOW_L]);
	CATS_ObjectDel(wk->pAct[NACT_ALLOW_R]);

	for(i = 0;i < PANEL_ACTMAX;i++){
		SafariLPanel_ObjDelete(wk->pAPanel[i]);
	}
}

/**
 *	@brief FontOamObjの作成
 */
static void sub_MakeFontOam(SAFARIL_WORK* wk)
{
	FONTOAM_INIT finit;
	int vram_size;
	int i,ofsx;

	//OAMDATAを作成
	wk->pFOamData = FONTOAM_OAMDATA_Make(&(wk->win_obj), wk->heapID);
	vram_size = FONTOAM_OAMDATA_NeedCharSize(wk->pFOamData, NNS_G2D_VRAM_TYPE_2DMAIN);

	ofsx = ((GF_BGL_BmpWinGet_SizeX(&wk->win_obj)*8) -
			FontProc_GetPrintStrWidth( FONT_SYSTEM,wk->msg.sel[MSGIDX_SEL_BACK], 0 ))/2;

	GF_STR_PrintColor( &(wk->win_obj),FONT_SYSTEM,
						wk->msg.sel[MSGIDX_SEL_BACK],
						ofsx,0,MSG_NO_PUT,GF_PRINTCOLOR_MAKE(0xB,0x06,0),NULL );

	finit.fontoam_sys = wk->pFOamSys;
	finit.bmp = &(wk->win_obj);
	finit.clact_set = CATS_GetClactSetPtr(wk->pActRes);
	finit.pltt = CATS_PlttProxy(wk->pActRes, 0);
	finit.bg_pri = BACK_PRI;
	finit.soft_pri = PANEL1_SPRI;
	finit.x = BMPL_BACK_PX*8;
	finit.y = BMPL_BACK_PY*8+4;
	finit.draw_area = NNS_G2D_VRAM_TYPE_2DMAIN;
	finit.heap = wk->heapID;

	for(i = 0;i < BMPL_OBJ_MAX;i++){
		CharVramAreaAlloc(vram_size,
			CHARM_CONT_OFFSET, NNS_G2D_VRAM_TYPE_2DMAIN,&(wk->pFontOam[i].cma));

		finit.char_ofs = wk->pFontOam[i].cma.alloc_ofs;
		finit.parent = NULL;//wk->pActWMan->pAct[EACT_PLATE1+i].act;

		wk->pFontOam[i].foam = FONTOAM_OAMDATA_Init(&finit, wk->pFOamData);
		FONTOAM_SetDrawFlag(wk->pFontOam[i].foam,TRUE);
		FONTOAM_SetPaletteNo(wk->pFontOam[i].foam,BMPL_OBJ_PAL);
	}
}

/**
 *	@brief	マーカー編集画面FontOamObjの削除
 */
static void sub_DelFontOam(SAFARIL_WORK* wk)
{
	int i;

	for(i = 0;i < BMPL_OBJ_MAX;i++){
		FONTOAM_OAMDATA_Delete(wk->pFontOam[i].foam);
		CharVramAreaFree(&(wk->pFontOam[i].cma));
		wk->pFontOam[i].foam = NULL;
	}
	FONTOAM_OAMDATA_Free(wk->pFOamData);
}


/**
 *	@brief	expウィンドウフレーム描画
 */
static void sub_ExpWinFrameDraw(SAFARIL_WORK* wk,u8 pat)
{
	//クリアしてから
	GF_BGL_ScrFill(wk->bgl,MFRM_MSG,0x0000,
		BMPL_WARN_PX-1,BMPL_WARN_PY,BMPL_WARN_SX+2,BMPL_WARN_SY,GF_BGL_SCRWRT_PALIN);

	//書き込み
	GF_BGL_ScrWriteExpand(wk->bgl,MFRM_MSG,
		BMPL_EXP_PX-1,BMPL_EXP_PY-(pat*2),
		BMPL_EXP_SX+2,BMPL_EXP_SY+(pat*2),wk->pSWin->rawData,0,pat*BMPL_EXP_SY,
		wk->pSWin->screenWidth/8,wk->pSWin->screenHeight/8);
	GF_BGL_LoadScreenV_Req(wk->bgl,MFRM_MSG);
}

/**
 *	@brief	expウィンドウメッセージ描画
 */
static void sub_ExpWinDraw(SAFARIL_WORK* wk,u8 widx,u8 midx,u8 draw_f)
{
	STRBUF** buf;
	u8 ofsy,ofsx,wsx,wsy;
	GF_PRINTCOLOR	col;

	wsx = (GF_BGL_BmpWinGet_SizeX(&wk->win[widx])*8);
	wsy = (GF_BGL_BmpWinGet_SizeY(&wk->win[widx])*8);

	col = FMSG_FCOL;
	if(widx != WIN_BACK){
		buf = wk->msg.exp;
		ofsx = 0;
		ofsy = 4;

		if(widx == WIN_EXP || widx == WIN_WARN){	//デザイン描画
			col = EXPMSG_FCOL;
			GF_BGL_BmpWinDataFill(&wk->win[widx],EXPMSG_COL_BACK);
			GF_BGL_BmpWinFill( &wk->win[widx],WIN_EXP_FRAME_COL,0,0,wsx,2);
			GF_BGL_BmpWinFill( &wk->win[widx],WIN_EXP_FRAME_COL,0,wsy-2,wsx,2);
		}else{
			GF_BGL_BmpWinDataFill(&wk->win[widx],FMSG_COL_BACK);
		}
	}else{
		buf = wk->msg.sel;
		ofsx = (wsx - FontProc_GetPrintStrWidth( FONT_SYSTEM,buf[midx], 0 ))/2;
		ofsy = 4;
	}

	GF_STR_PrintColor(&wk->win[widx],FONT_SYSTEM,
		buf[midx],ofsx,ofsy,MSG_ALLPUT,col,NULL);
}

/*
 *	@brief	ページ描画コントロール
 */
static void sub_PageViewSet(SAFARIL_WORK* wk)
{
	int ofsx;

	GF_BGL_BmpWinDataFill(&wk->win[WIN_PAGE],FMSG_COL_BACK);

	WORDSET_RegisterNumber(wk->msg.wset,0,wk->page+1,1,NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT);
	WORDSET_ExpandStr(wk->msg.wset,wk->msg.tmp,wk->msg.page);

	ofsx = ((GF_BGL_BmpWinGet_SizeX(&wk->win[WIN_PAGE])*8) -
			FontProc_GetPrintStrWidth( FONT_SYSTEM,wk->msg.tmp, 0 ))/2;

	GF_STR_PrintColor(&wk->win[WIN_PAGE],FONT_SYSTEM,
		wk->msg.tmp,ofsx,0,MSG_ALLPUT,FMSG_FCOL,NULL);

	GF_BGL_LoadScreenV_Req(wk->bgl,MFRM_BACK);

	//矢印アクター表示状態セット
	CLACT_SetAnmFlag(wk->pAct[NACT_ALLOW_L],FALSE);
	if(wk->page == 0){
		CLACT_AnmChg(wk->pAct[NACT_ALLOW_L],ANM_N_ALLOW_LF);
	}else{
		CLACT_AnmChg(wk->pAct[NACT_ALLOW_L],ANM_N_ALLOW_L);
	}
	CLACT_SetAnmFlag(wk->pAct[NACT_ALLOW_R],FALSE);
	if(wk->page == (PAGE_MAX-1)){
		CLACT_AnmChg(wk->pAct[NACT_ALLOW_R],ANM_N_ALLOW_RF);
	}else{
		CLACT_AnmChg(wk->pAct[NACT_ALLOW_R],ANM_N_ALLOW_R);
	}
}

/**
 *	@brief	カーソルパターンセット
 */
static void sub_CursorVisibleSet(SAFARIL_WORK* wk,u8 idx,u8 pat,BOOL draw_f)
{
	if(!draw_f){
		CLACT_SetDrawFlag(wk->pAct[idx],FALSE);
		return;
	}
	CLACT_SetDrawFlag(wk->pAct[idx],TRUE);
	CLACT_AnmChg(wk->pAct[idx], pat);
}

/**
 *	@brief	カーソル表示位置セット
 */
static void sub_CursorPosSet(SAFARIL_WORK* wk,u8 idx,u8 pos)
{
	u8 pat = 0;
	s16 px,py;

	if(pos >= PANEL_VIEW_MAX){
		px = BACK_BTN_PX;
		py = BACK_BTN_PY;
		pat = ANM_N_BCURSOR;
	}else if(idx != NACT_CURSOR_C){
		px = PANEL1_PX+((pos%3)*PANEL1_OX);
		py = PANEL1_PY+((pos/3)*PANEL1_OY);
		if(idx == NACT_CURSOR_S){
			py += (BGU_SCRL_H);
		}else if(idx == NACT_CURSOR_A){
			pat = ANM_N_FCURSOR;
		}else if(idx == NACT_CURSOR && wk->mode == MODE_SORT){
			pat = ANM_N_ACURSOR;
		}
	}else{
		px = PANEL2_PX+((pos%3)*PANEL2_OX);
		py = PANEL2_PY+((pos/3)*PANEL2_OY);
	}
	CATS_ObjectPosSet(wk->pAct[idx], px, py);
	sub_CursorVisibleSet(wk,idx,pat,TRUE);
}

/**
 *	@brief	スクリーンフィルタセット
 */
static void sub_ScrnFilterSet(SAFARIL_WORK* wk,u8 frame,u8 pos,BOOL on_f)
{
	u8 px,py;

	if(!on_f){
		GF_BGL_ScrClear(wk->bgl,frame);
		GF_BGL_LoadScreenV_Req(wk->bgl,frame);
		return;
	}
	//初期描画
	GF_BGL_ScrWriteExpand(wk->bgl,frame,
		0,wk->mode*BG_SCRL_HC,32,BG_SCRL_HC,wk->pSFl->rawData,0,0,
		wk->pSFl->screenWidth/8,wk->pSFl->screenHeight/8);

	if(wk->mode == MODE_BLOCK){
		px = PANEL1_CPX+((pos%3)*PANEL1_COX);
		py = PANEL1_CPY+((pos/3)*PANEL1_COY);

		if(frame == SFRM_FILTER){
			GF_BGL_ScrWriteExpand(wk->bgl,frame,
				0,21,32,3,wk->pSFl->rawData,0,24,
				wk->pSFl->screenWidth/8,wk->pSFl->screenHeight/8);
//			GF_BGL_ScrFill(wk->bgl,frame,0x0000,
//				0,21,32,3,GF_BGL_SCRWRT_PALIN);
		}
	}else{
		px = PANEL2_CPX+((pos%3)*PANEL2_COX);
		py = PANEL2_CPY+((pos/3)*PANEL2_COY)+BG_SCRL_HC;
	}

	GF_BGL_ScrFill(wk->bgl,frame,0x0000,
					px,py,PANEL_CSX,PANEL_CSY,GF_BGL_SCRWRT_PALIN);

	GF_BGL_LoadScreenV_Req(wk->bgl,frame);
}

/**
 *	@brief	上画面パネルスクリーン作成
 */
static void sub_MakePanelScreen(SAFARIL_WORK* wk)
{
	int i,j,k,no;
	u8 px,py;

	no = BGU_PANELCHAR_BASE;
	for(i = 0;i < PANEL_VIEW_MAX;i++){
		px = PANEL1_CPX+((i%3)*PANEL1_COX);
		py = PANEL1_CPY+((i/3)*PANEL1_COY);

		for(j = 0;j < PANEL_CSY;j++){
			for(k = 0;k < PANEL_CSX;k++){
				GF_BGL_ScrFill(wk->bgl,SFRM_PANEL,no++,
						px+k,py+j,1,1,GF_BGL_SCRWRT_PALIN);

			}
		}
	}
	GF_BGL_LoadScreenV_Req(wk->bgl,SFRM_PANEL);
}

/**
 *	@brief	上画面パネルスクリーン用キャラクタバッファ作成
 */
static void sub_SetPanelChar(SAFARIL_WORK* wk,ARCHANDLE* handle,u8 pos,u8 panel)
{
	void* buf;
	NNSG2dCharacterData* pChar;

	buf = ArcUtil_HDL_CharDataGet(handle,
			NARC_safaril_gra_panel01_ncgr+panel,FALSE,&pChar, wk->heapID);

	GF_BGL_LoadCharacter(wk->bgl,SFRM_PANEL,
		pChar->pRawData,PANEL_CHAR_SIZ,64*pos+BGU_PANELCHAR_BASE);
	sys_FreeMemoryEz(buf);
}

/**
 *	@brief	上画面パネルスクリーン用キャラクタバッファ作成　初回
 */
static void sub_MakePanelCharBuffer(SAFARIL_WORK* wk,u8 pos,u8 panel)
{
	int i;
	ARCHANDLE* handle;

	//アーカイブハンドル取得
	handle = ArchiveDataHandleOpen(ARC_SAFARIL_GRA,wk->heapID);

	if(pos >= PANEL_VIEW_MAX){
		for(i = 0;i < PANEL_VIEW_MAX;i++){
			sub_SetPanelChar(wk,handle,i,wk->layout.block[i].map_id);
		}
	}else{
		sub_SetPanelChar(wk,handle,pos,panel);
	}
	//ハンドル閉じる
	ArchiveDataHandleClose( handle );

	//キャラクタバッファ転送
//	GF_BGL_LoadCharacter(wk->bgl,SFRM_PANEL,
//		wk->pCBufPanel,PANEL_CBUF_SIZ,1);
}


/**
 *	@brief	初期画面描画
 */
static void sub_InitialDraw(SAFARIL_WORK* wk)
{
	sub_BlockInfoWinBaseDraw(wk);
	sub_BlockInfoWinDraw(wk,wk->pos);

	sub_ExpWinFrameDraw(wk,0);
	sub_ExpWinDraw(wk,WIN_EXP,MSGIDX_EXP_DO,TRUE);

	if(wk->key_mode == APP_KTST_KEY){
		imodechg_to_key(wk);
	}else{
		imodechg_to_touch(wk);
	}
	//上画面はスクロールアウトさせておく
	wk->scrl_m = 0;
	wk->scrl_s = -BGU_SCRL_H;
	wk->scrl_f = TRUE;
}

/**
 *	@brief	上画面ガイドベース表示描画
 */
static void sub_BlockInfoWinBaseDraw(SAFARIL_WORK* wk)
{
	int i,id;
	GF_BGL_BMPWIN* win;

	if(!wk->obj_place_f){
		return;
	}
	for(i = 0;i < SAFARI_ATTR_MAX;i++){
		win = &(wk->win[WIN_INFO_01_01+(i*2)]);
		MSGMAN_GetString(wk->msg.man,msg_safaril_info01+i,wk->msg.tmp);
		GF_STR_PrintColor( win,FONT_SYSTEM,wk->msg.tmp,
			0,0,MSG_ALLPUT,GetInfoCol(i%2),NULL );
		GF_BGL_BmpWinOn(win);
	}
}

/**
 *	@brief	上画面ガイド表示描画
 */
static void sub_BlockInfoWinDraw(SAFARIL_WORK* wk,u8 block_id)
{
	int i;
	SAFARI_BLOCK* pb;
	GF_BGL_BMPWIN* win;
	u8	num[SAFARI_ATTR_MAX];

	if(block_id >= PANEL_VIEW_MAX){
		if(wk->obj_place_f){
			for(i = 0;i < SAFARI_ATTR_MAX;i++){
				win = &(wk->win[WIN_INFO_01_01+(i*2)+1]);

				GF_BGL_BmpWinDataFill( win, GetInfoBaseCol(i%2));
				GF_BGL_BmpWinOnVReq(win);
			}
		}
		GF_BGL_BmpWinDataFill(&wk->win[WIN_INFO_00], INFO_FCOL0_BASE);
		GF_BGL_BmpWinOnVReq(&wk->win[WIN_INFO_00]);
		return;
	}
	pb = &wk->layout.block[block_id];

	//ブロック名表示
	{
		u8 ofsx;
		win = &(wk->win[WIN_INFO_00]);
		MSGMAN_GetString(wk->msg.man,msg_safaril_area01+pb->map_id,wk->msg.tmp);
		ofsx = ((GF_BGL_BmpWinGet_SizeX(win)*8) -
				FontProc_GetPrintStrWidth( FONT_SYSTEM,wk->msg.tmp, 0 ))/2;

		GF_BGL_BmpWinDataFill( win, INFO_FCOL0_BASE);
		GF_STR_PrintColor( win,FONT_SYSTEM,wk->msg.tmp,
			ofsx,4,MSG_NO_PUT,INFO_FCOL0,NULL );
		GF_BGL_BmpWinOnVReq(win);
	}

	if(!wk->obj_place_f){
		return;
	}

	MI_CpuClear8(num,sizeof(u8)*SAFARI_ATTR_MAX);
	for( i = 0;i < pb->obj_ct; i++){
		SAFARI_OBJ*	obj;
		SAFARI_OBJ_PARAM	prm;

		obj = &pb->obj[i];
		SafariData_GetObjectParam(&prm,obj->id,PM_NEUTRAL);	//性別気にしなくて良いの固定値にしてます
		if(prm.attr == SAFARI_ATTR_NONE){
			num[4]++;
		}else{
			num[prm.attr-1]++;
		}
	}
	for(i = 0;i < SAFARI_ATTR_MAX;i++){
		win = &(wk->win[WIN_INFO_01_01+(i*2)+1]);

		WORDSET_RegisterNumber(wk->msg.wset,0,num[i],2,NUMBER_DISPTYPE_SPACE, NUMBER_CODETYPE_DEFAULT);
		WORDSET_ExpandStr(wk->msg.wset,wk->msg.tmp,wk->msg.obj_num);
		GF_BGL_BmpWinDataFill( win, GetInfoBaseCol(i%2));
		GF_STR_PrintColor( win,FONT_SYSTEM,wk->msg.tmp,
			0,0,MSG_NO_PUT,GetInfoCol(i%2),NULL );
		GF_BGL_BmpWinOnVReq(win);
	}
}

/**
 *	@brief	上画面ガイド表示描画 ブロックチェンジ時
 */
static void sub_BlockInfoWinChangeDraw(SAFARIL_WORK* wk)
{
	u8 ofsx;
	SAFARI_BLOCK* pb;
	GF_BGL_BMPWIN* win;

	win = &(wk->win[WIN_INFO_00]);
	GF_BGL_BmpWinDataFill(win, INFO_FCOL0_BASE);

	//ブロック名表示
	pb = &wk->layout.block[wk->pos];
	MSGMAN_GetString(wk->msg.man,msg_safaril_area01+pb->map_id,wk->msg.tmp);
	ofsx = ( 80 - FontProc_GetPrintStrWidth( FONT_SYSTEM,wk->msg.tmp, 0 ))/2;
	GF_STR_PrintColor( win,FONT_SYSTEM,wk->msg.tmp,ofsx,4,MSG_NO_PUT,INFO_FCOL0,NULL );

	//チェンジ候補
	if(wk->c_pos < PANEL_VIEW_MAX){
		MSGMAN_GetString(wk->msg.man,msg_safaril_area01+wk->c_pos+(wk->page*SAFARIL_BLOCK_MAX),wk->msg.tmp);
		ofsx = ( 80 - FontProc_GetPrintStrWidth( FONT_SYSTEM,wk->msg.tmp, 0 ))/2;
		GF_STR_PrintColor( win,FONT_SYSTEM,wk->msg.tmp,ofsx+(18*8),4,MSG_NO_PUT,INFO_FCOL0,NULL );
	}
	GF_BGL_BmpWinOnVReq(win);
}

//////////////////////////////////////////////////////////////////////////////////
//ブロック画面サブ
/**
 *	@brief	ブロック取替え画面移行スクロールパラメータ初期化
 */
static void eff_BCScrollInit(SAFARIL_WORK* wk)
{
	if(wk->mode == MODE_BLOCK){
		//フィルタ一部解除
		GF_BGL_ScrFill(wk->bgl,MFRM_FILTER,0x0000,0,BMPL_EXP_PY,32,BMPL_EXP_SY,GF_BGL_SCRWRT_PALIN);
		GF_BGL_LoadScreenV_Req(wk->bgl,MFRM_FILTER);

		//上画面スクロール初期値設定
		wk->scrl_s = -BGU_SCRL_H;
		wk->scrl_f = TRUE;

		wk->c_pos = 0;
//		if(wk->key_mode == APP_KTST_KEY){
			sub_CursorPosSet(wk,NACT_CURSOR_C,wk->c_pos);
			sub_CursorPosSet(wk,NACT_CURSOR_S,wk->pos);
//		}

		//ページナンバー表示
		sub_PageViewSet(wk);
	}else{
		int i,idx;

		//下画面フィルタ解除
		sub_ScrnFilterSet(wk,MFRM_FILTER,0xFF,FALSE);
		sub_CursorVisibleSet(wk,NACT_CURSOR_C,0,FALSE);

		//下画面ブロックアクターセット
		idx = wk->panel_sw*PANEL_VIEW_MAX;
		for(i = 0;i < PANEL_VIEW_MAX;i++){
			CATS_ObjectPosSetCap(wk->pAPanel[i+idx],
				PANEL1_PX+((i%3)*PANEL1_OX),
				PANEL1_PY+((i/3)*PANEL1_OY)-BGD_SCRL_H);
			CATS_ObjectBGPriSetCap(wk->pAPanel[i+idx],2);
			SafariLPanel_ResChange(wk->panel,i+idx,wk->layout.block[i].map_id);
		}
	}
	//スクロールパラメータ初期化
	wk->scr_ct = 0;

	GF_BGL_BmpWinDataFill( &(wk->win[WIN_INFO_00]), INFO_FCOL0_BASE);
	GF_BGL_BmpWinOnVReq(&(wk->win[WIN_INFO_00]));

	//BTS1251 OBJ回り込み防止
	G2S_SetWnd0Position( 0 , 0 , 240 , 16 );
	G2S_SetWnd0InsidePlane( GX_WND_PLANEMASK_BG0 |
							GX_WND_PLANEMASK_BG1 |
							GX_WND_PLANEMASK_BG2 |
							GX_WND_PLANEMASK_BG3 , TRUE );
	G2S_SetWndOutsidePlane( GX_WND_PLANEMASK_BG0 |
							GX_WND_PLANEMASK_BG1 |
							GX_WND_PLANEMASK_BG2 |
							GX_WND_PLANEMASK_BG3 |
							GX_WND_PLANEMASK_OBJ , TRUE );
	GXS_SetVisibleWnd( GX_WNDMASK_W0 );
}

/**
 *	@brief	ブロック取替え画面移行スクロール処理
 */
static BOOL eff_BCScroll(SAFARIL_WORK* wk)
{
	int i,dif;

	if(wk->mode == MODE_BLOCK){
		wk->scrl_m += BG_SCRL_DIF;
		wk->scrl_s += BG_SCRL_DIF;
		dif = -BG_SCRL_DIF;
	}else{
		wk->scrl_m -= BG_SCRL_DIF;
		wk->scrl_s -= BG_SCRL_DIF;
		dif = BG_SCRL_DIF;
	}
	wk->scrl_f = TRUE;

	for(i = 0;i < PANEL_VIEW_MAX;i++){
		CATS_ObjectPosMoveCap(wk->pAPanel[i+(wk->panel_sw*PANEL_VIEW_MAX)],0,dif);
	}
	CATS_ObjectPosMove(wk->pAct[NACT_CURSOR],0,dif);
	CATS_ObjectPosMove(wk->pAct[NACT_CURSOR_S],0,dif);

	if(++wk->scr_ct >= BG_SCRL_TIME){
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	ブロックチェンジ画面　ページスクロール初期化
 */
static void eff_PageScrollInit(SAFARIL_WORK* wk)
{
	int i,idx,ox;

	if(wk->scrl_d == SCRL_LEFT){
		wk->page += 1;
		ox = PANEL_SCRL_W;
	}else{
		wk->page -= 1;
		ox = -PANEL_SCRL_W;
	}
	idx = (wk->panel_sw)*PANEL_VIEW_MAX;

	for(i = 0;i < PANEL_VIEW_MAX;i++){
		CATS_ObjectPosSetCap(wk->pAPanel[i+idx],
			PANEL2_PX+((i%3)*PANEL2_OX)+ox,
			PANEL2_PY+((i/3)*PANEL2_OY));
		SafariLPanel_ResChange(wk->panel,i+idx,wk->page*PANEL_VIEW_MAX+i);
		CATS_ObjectBGPriSetCap(wk->pAPanel[i+idx],3);
	}
	//スクロールパラメータ初期化
	wk->scr_ct = 0;
}

/**
 *	@brief	ブロックチェンジ画面　ページスクロール
 */
static BOOL eff_PageScroll(SAFARIL_WORK* wk)
{
	int i,ox;

	if(wk->scrl_d == SCRL_LEFT){
		ox = -PANEL_SCRL_DIF;
	}else{
		ox = PANEL_SCRL_DIF;
	}

	for(i = 0;i < PANEL_VIEW_MAX*2;i++){
		CATS_ObjectPosMoveCap(wk->pAPanel[i],ox,0);
	}
	if(++wk->scr_ct >= PANEL_SCRL_TIME){
		wk->scr_ct = 0;
//		if(wk->key_mode == APP_KTST_KEY){
			sub_CursorPosSet(wk,NACT_CURSOR_C,wk->c_pos);
			sub_BlockInfoWinChangeDraw(wk);
//		}
		sub_PageViewSet(wk);
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	エフェクト　パネルチェンジ
 */
static BOOL eff_PanelChange(SAFARIL_WORK* wk,u8 dir)
{
	int i,idx;
	u8	id[2],pos[2];
	static const s8 tbl[][2] = {
	 {-1,1},{1,-1}
	};
	idx = wk->panel_sw*PANEL_VIEW_MAX;

	id[0] = wk->pos + idx;
	id[1] = wk->c_pos + idx;
	pos[0] = wk->pos/3;
	pos[1] = wk->c_pos/3;

	for(i = 0;i < 2;i++){
		CATS_ObjectPosMoveCap(wk->pAPanel[id[i]],0,SORT_SCRL_DIF*tbl[dir][pos[i]]);
	}
	if(++wk->scr_ct >= SORT_SCRL_TIME){
		wk->scr_ct = 0;
		return TRUE;
	}
	return FALSE;
}